package org.itboys.mobile.service.mongo.front;

import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.wallet.SundayAccountDto;
import org.itboys.mobile.entity.mongo.SundayAccount;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2017/12/18.
 * 圣代_提现账户_service
 */
@Service
public class SundayFrontAccountService extends BaseMongoService<SundayAccount>{

    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAccount> getEntityClass() {
        return SundayAccount.class;
    }

    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SystemDictionaryService systemDictionaryService;

    /**
     * 保存提现账户
     * @param id
     * @param memberId
     * @param type
     * @param realName
     * @param phone
     * @param openingBank
     * @param account
     */
    public void saveAccount(Long id,Long memberId,int type,String realName,String phone,String bankName,String logo,String openingBank,String account){
        SundayAccount sundayAccount = new SundayAccount();
        sundayAccount.setId(id);
        sundayAccount.setMemberId(memberId);
        sundayAccount.setType(type);
        sundayAccount.setRealName(realName);
        sundayAccount.setPhone(phone);
        sundayAccount.setOpeningBank(openingBank);
        sundayAccount.setAccount(account);
        sundayAccount.setBankName(bankName);
        sundayAccount.setLogo(logo);

        if(id == null || id == 0L){
            super.save(sundayAccount);
        }else{
            super.updateExceptEmpty(id,sundayAccount);
        }
    }

    /**
     * 根据memberId获取(提现)账户dto
     * @param memberId
     * @return
     */
    public List<SundayAccountDto> select(Long memberId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("sort","id");
        param.put("order","desc");
        List<SundayAccount> accountList = super.list(param);

        //组装dto
        List<SundayAccountDto> list = packageDtoMuilt(accountList);

        return list;
    }

    /**
     * 组装dto(多)
     * @param accountList
     * @return
     */
    public List<SundayAccountDto> packageDtoMuilt(List<SundayAccount> accountList){
        List<SundayAccountDto> list = new ArrayList<SundayAccountDto>();

        String alipayLogoUrl = systemDictionaryService.getDictionaryByKey2("cash_account_alipay_logo").getImage();
        String weixinLogoUrl = systemDictionaryService.getDictionaryByKey2("cash_account_weixin_logo").getImage();
        for(SundayAccount account : accountList){
            if(account.getType() ==2){//支付宝
                account.setLogo(alipayLogoUrl);
            }else if(account.getType() ==3){
                account.setLogo(weixinLogoUrl);
            }
            SundayAccountDto dto = packageDtoSingle(account);
            list.add(dto);
        }
        return list;
    }

    /**
     * 组装dto(单)
     * @param account
     * @return
     */
    public SundayAccountDto packageDtoSingle(SundayAccount account){
        String imgRoot = resourceHolder.getStringValue("imgRoot");

        SundayAccountDto dto = new SundayAccountDto();
        dto.setId(account.getId());
        dto.setType(account.getType());
        dto.setBankName(account.getBankName());
        dto.setLogo(imgRoot+account.getLogo());
        dto.setAccount(account.getAccount());
        return dto;
    }

    /**
     * 获取单条提现账户dto
     * @param id
     * @return
     */
    public SundayAccountDto getSingleAccountDto(Long id){
        SundayAccount account = null;
        if(id == 0){
            account = new SundayAccount();
            //新增id默认为0,账户类型默认为银行
            account.setId(0L);
            account.setType(1);
        }else{
            account = super.getById(id);
        }
        SundayAccountDto dto = packageDtoSingle(account);
        return dto;
    }
}
